package com.yanqu.road.dao.impl.cross.cricket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.court.CricketCourtGroupData;
import com.yanqu.road.entity.cricket.court.CricketCourtGroupMatchData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCricketCourtGroupMatchDaoImpl extends TempDataObjectDao<CricketCourtGroupMatchData> {

    public Map<Integer, CricketCourtGroupMatchData> getCricketCourtGroupMatchDataMap(int round) {
        Map<Integer, CricketCourtGroupMatchData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_cricket_court_group_match_data where round = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            int i = 1;
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(i++, round);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtGroupMatchData data = parse(rs);
                map.put(data.getMatchGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String insertSql = "insert into `t_c_cricket_court_group_match_data`(round,courtId,matchGroupId,serverGroupIdList,serverOpenDayIndex,groupLimit,updateTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CricketCourtGroupMatchData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerGroupIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getServerOpenDayIndex()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupLimit()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update `t_c_cricket_court_group_match_data` set `serverGroupIdList`=?,`serverOpenDayIndex`=?,`groupLimit`=?,`updateTime`=? where round=? and courtId=? and matchGroupId=?";

    @Override
    protected boolean updateData(CricketCourtGroupMatchData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getServerGroupIdList())));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getServerOpenDayIndex()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupLimit()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketCourtGroupMatchData parseData(ResultSet rs) throws SQLException {
        CricketCourtGroupMatchData data = new CricketCourtGroupMatchData();
        data.setRound(rs.getInt("round"));
        data.setCourtId(rs.getInt("courtId"));
        data.setMatchGroupId(rs.getInt("matchGroupId"));
        data.setServerGroupIdList(JSON.parseObject(rs.getString("serverGroupIdList"), new TypeReference<List<Integer>>() {

        }));
        data.setServerOpenDayIndex(rs.getInt("serverOpenDayIndex"));
        data.setGroupLimit(rs.getInt("groupLimit"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
